const db = require('../config/db.js');
const inFile = require('../utils/inFile.js');
const carModel = {
    //带分页查询,模糊查询
    GetCarList: async ({pageCurrent,pageSize,CarKeyword}) =>{
        console.log(pageCurrent,'当前页');
        console.log(pageSize,'每页显示的条数');
        let current = (pageCurrent - 1) * pageSize || 0;
        let size = pageSize || 10;
        let sql1 = 'SELECT COUNT(*) AS total FROM car;';
        let sql2 = `SELECT COUNT(*) AS total FROM car WHERE CONCAT(brand,info,model,place) LIKE CONCAT('%', ?, '%')`;
        let sql3 = `SELECT id,brand,info,place,state,model,rent,pattern,image FROM car LIMIT ${current},${size}`;
        let sql4 = `SELECT id,brand,info,place,state,model,rent,pattern,image FROM car WHERE CONCAT(brand,info,model,place) LIKE CONCAT('%', ?, '%') LIMIT ${current},${size};`
        try {
            if(CarKeyword) {

                const res1 = await db.query(sql4, [CarKeyword]);
                const res2 = await db.query(sql2,[CarKeyword]);
                const [{total}]= res2[0];
                console.log(total, '获取汽车列表总记录');
                console.log(res1[0], '获取汽车列表记录分页带模糊查询');
                return {
                    list: res1[0],
                    total,
                }
            }else{
                const res1 = await db.query(sql1);
                const res2 = await db.query(sql3);
                const [{total}]= res1[0];
                console.log(total, '获取汽车列表总记录');
                console.log(res2[0], '获取汽车列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        } catch (error) {
            console.log(error,'model层汽车列表数据出错啦');
        }
    },
    AddCar:async({brand,info,place,model,rent,pattern,state,image},file)=>{
        console.log(brand,info,place,model,rent,pattern,state,image);
        const sql1 = "SELECT * FROM car WHERE model = ? ";
        const sql2 = "INSERT INTO car (brand,info,place,model,rent,pattern,state,image) VALUES (?,?,?,?,?,?,?,?);";
        const sql3 = "INSERT INTO car (brand,info,place,model,rent,pattern,state) VALUES (?,?,?,?,?,?,?);";
        try {
            if (file) {
                const imgPath = inFile(file,'img/Car/');
                if (model) {
                    const result1 = await db.query(sql1, [model]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql2, [brand,info,place,model,rent,pattern,state,imgPath]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }else{
                if (model) {
                    const result1 = await db.query(sql1, [model]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql3, [brand,info,place,model,rent,pattern,state]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }
        } catch (error) {
            console.log(error, 'CarModel添加汽车失败啦');
        }
    },
    EditCar :async ({id,brand,info,place,model,rent,pattern,state,image},file) => {
        const sql1 = "UPDATE car SET brand=?,info=?,place=?,model=?,rent=?,pattern=?,state=?,image=? WHERE id=?;";
        const sql2 = "UPDATE car SET brand=?,info=?,place=?,model=?,rent=?,pattern=?,state=? WHERE id=?;";
        try {
            if (file) {
                const imgPath = inFile(file,'img/Car/');
                if (id) {
                    const result2 = await db.query(sql1, [brand,info,place,model,rent,pattern,state,imgPath,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }else{
                if (id) {
                    const result2 = await db.query(sql2, [brand,info,place,model,rent,pattern,state,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }
        } catch (error) {
            console.log(error, 'carModel修改汽车失败啦');
        }
    },
    DeleteCar: async ({id})=>{
        console.log(id,'删除汽车id测试');
        const sql ='DELETE FROM car WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'删除汽车调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    BatchDeleteCar: async  ({idList}) => {
        console.log(idList,'收集idList的数组');
        const sql = `DELETE FROM car WHERE id IN(${idList});`;
        try {
            if (idList.length>0){
                const res = await db.query(sql);
                console.log(res,'Model层批量删除汽车调试');
                return res[0];
            }else{
                return ;
            }
        }catch (error){
            return{
                errorMsg:error
            }
        }
    },
    GetCarDetail:async ({id}) => {
        console.log(id,'汽车id');
        const sql = 'SELECT id,brand,info,place,state,model,rent,pattern,image FROM car WHERE id = ? ;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'获取汽车详情调试');
                return res[0];
            }else{
                return;
            }
        }catch (error) {
            console.log(error,'获取汽车详情出错了——Model层');
        }
    }
};

module.exports = carModel;